/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlisinhvien.pl;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import quanlisinhvien.bll.SinhVienBLL;
import quanlisinhvien.dto.SinhVienDTO;


/**
 *
 * @author Marshmallow
 */
public class F_DiemSinhVien extends JDialog{
    private SinhVienDTO sinhVien;
    private SinhVienBLL qlSinhVien;
    private JPanel panel;
    private JTextField txtDiem1, txtDiem2, txtDiem3;
    private JLabel lblKetQua;
    public F_DiemSinhVien(JFrame parent,SinhVienDTO _sinhVien) {
        super(parent,"", true);
        this.sinhVien = new SinhVienDTO(_sinhVien);
        this.qlSinhVien = new SinhVienBLL();
        this.panel = new JPanel(new GridLayout(5, 1,10,10));
        
        txtDiem1 = new JTextField(10);
        JPanel dong1 = new JPanel();
        dong1.add(new JLabel("Điểm môn 1:"));
        dong1.add(txtDiem1);
        dong1.add(new JLabel());
       
        txtDiem2 = new JTextField(10);
        JPanel dong2 = new JPanel();
        dong2.add(new JLabel("Điểm môn 2:"));
        dong2.add(txtDiem2);
        dong2.add(new JLabel());
        
        txtDiem3 = new JTextField(10);
        JPanel dong3 = new JPanel();
        dong3.add(new JLabel("Điểm môn 3:"));
        dong3.add(txtDiem3);
        dong3.add(new JLabel());
        
        JButton btnSua = new JButton("Lưu điểm");
        btnSua.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
               try{
                    lblKetQua.setText("");
                    sinhVien.setDiemMon1(Float.parseFloat(txtDiem1.getText()));
                    sinhVien.setDiemMon2(Float.parseFloat(txtDiem2.getText()));
                    sinhVien.setDiemMon3(Float.parseFloat(txtDiem3.getText()));
                    if (qlSinhVien.CapNhatSinhVien(sinhVien))
                        JOptionPane.showMessageDialog(rootPane, "Cập nhật điểm thành công");
               }
               catch(Exception ex){
                   JOptionPane.showMessageDialog(rootPane, ex.getMessage());
               }
            }
        });
        
        JButton btnXepLoai = new JButton("Xem xếp loại");
        btnXepLoai.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                float diemTB = qlSinhVien.TinhDiemTrungBinh(sinhVien);
                String xepLoai = qlSinhVien.XepLoai(sinhVien);
                lblKetQua.setText("Điểm trung bình: " + diemTB + "-Xếp loại: " + xepLoai);
            }
        });
        
        JPanel dong4 = new JPanel();
        dong4.add(btnSua);
        dong4.add(btnXepLoai);
        
        lblKetQua = new JLabel("Xếp loại:");
        JPanel dong5 = new JPanel();
        dong5.add(lblKetQua);
        
        panel.add(dong1);
        panel.add(dong2);
        panel.add(dong3);
        panel.add(dong4);
        panel.add(dong5);
            
        this.getContentPane().add(panel);
        this.setSize(400,250);
        this.setLocation(230,70);
       
        HienThiDiem();
    }
    private void HienThiDiem() {
        txtDiem1.setText(String.valueOf(this.sinhVien.getDiemMon1()));
        txtDiem2.setText(String.valueOf(this.sinhVien.getDiemMon2()));
        txtDiem3.setText(String.valueOf(this.sinhVien.getDiemMon3()));
    }
}
